package my.peerencrypt;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ClientMain extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8709709281534767913L;
	
	private JTextField textField1 = new JTextField("");
	private JTextField textField2 = new JTextField("");
	private JTextField textField3 = new JTextField("");
	private JTextField textField4 = new JTextField("");
	
	private String registrandom;
	
	private JButton genButton = new JButton("生成");
	private JButton regButton = new JButton("注册");

	String userName = "";
	String password = "";
	
	private Client client = new Client();
	public ClientMain() {
		super("对称加密-客户端");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// String laf = UIManager.getCrossPlatformLookAndFeelClassName();
		String laf = UIManager.getSystemLookAndFeelClassName();
		try {
			UIManager.setLookAndFeel(laf);
		} catch (UnsupportedLookAndFeelException exc) {
			System.err.println("Warning: UnsupportedLookAndFeel: " + laf);
		} catch (Exception exc) {
			System.err.println("Error loading " + laf + ": " + exc);
		}

		//Container c = getContentPane();
		//GridLayout layout = new GridLayout(3,2);
		//c.setLayout(layout);
		
		JLabel userNaemLabel = new JLabel("用户名:");
		JLabel passwordLabel = new JLabel("密  码:");
		final JTextField userNameText = new JTextField("XM001");
		userNameText.setColumns(20);
		final JTextField passwordText = new JTextField("123");
		passwordText.setColumns(30);
		JButton loginBtn = new JButton("登录");
		JButton registBtn = new JButton("注册");
		GridLayout labelLayout = new GridLayout(3,1);
		labelLayout.setVgap(10);
		labelLayout.setHgap(10);
		JPanel labelPanel = new  JPanel(labelLayout);
		JPanel fieldPanel = new  JPanel(labelLayout);
		JPanel buttonPanel = new JPanel(new GridLayout(1,2));
		labelPanel.add(userNaemLabel);
		fieldPanel.add(userNameText);
		labelPanel.add(passwordLabel);
		fieldPanel.add(passwordText);
		buttonPanel.add(loginBtn);
		buttonPanel.add(registBtn);
		
		JPanel formPanel = new JPanel();
		formPanel.add(labelPanel,BorderLayout.WEST);
		formPanel.add(fieldPanel,BorderLayout.CENTER);
		
		getContentPane().add(formPanel,BorderLayout.NORTH);
		getContentPane().add(buttonPanel,BorderLayout.CENTER);
		loginBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String userName = userNameText.getText();
				String password = passwordText.getText();
				int r = client.login(userName, password);
				if(r==0){
					JOptionPane.showMessageDialog(ClientMain.this,
							"登录成功。","提示",JOptionPane.WARNING_MESSAGE);
				} else {
					JOptionPane.showMessageDialog(ClientMain.this,
							"登录失败。","提示",JOptionPane.WARNING_MESSAGE);
				}

			}
		});
		
		GridLayout gridLayout = new GridLayout(1,4);
		gridLayout.setHgap(10);
		
		FlowLayout flayout = new FlowLayout();
		
		
		GridLayout gridLayout2 = new GridLayout(1,2);
		gridLayout.setHgap(20);
		
		final JPanel jp1 = new JPanel(flayout);
		jp1.add(new JLabel("注册码"));
		final JPanel jp = new JPanel(gridLayout);
	
		jp.add(textField1);
		jp.add(textField2);
		jp.add(textField3);
		jp.add(textField4);
		
		jp.setSize(200, 60);
		
		gridLayout2.setHgap(20);
		final JPanel jp2 = new JPanel(gridLayout2);
		jp2.add(genButton);
		jp2.add(regButton);
		
		genButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				refreshRegcode();
				
			}
		});
		
		
		regButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				userName = userNameText.getText();
				password = passwordText.getText();
				
				if(userName==null || "".equals(userName)){
					JOptionPane.showMessageDialog(ClientMain.this,
							"用户名为空。","提示",JOptionPane.WARNING_MESSAGE);
					return ;
				}
				if(password==null || "".equals(password)){
					JOptionPane.showMessageDialog(ClientMain.this,
							"密码为空。","提示",JOptionPane.WARNING_MESSAGE);
					return ;
				}
				if(registrandom==null || "".equals(registrandom)){
					JOptionPane.showMessageDialog(ClientMain.this,
							"请先生成注册码。","提示",JOptionPane.WARNING_MESSAGE);
					return ;
				}
				
				
				int r = client.regist(userName, password,registrandom);
				if(r==2){
					JOptionPane.showMessageDialog(ClientMain.this,
							"注册成功。","提示",JOptionPane.WARNING_MESSAGE);
				} else {
					JOptionPane.showMessageDialog(ClientMain.this,
							"注册失败。","提示",JOptionPane.WARNING_MESSAGE);
				}
				
			}
		});
		
		final JDialog dialog = new JDialog(this);
		GridLayout dialogGridLayout = new GridLayout(3, 1);
		dialogGridLayout.setVgap(10);
		dialog.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		dialog.setBounds(500,450,350, 150);
		dialog.setLayout(dialogGridLayout);
		dialog.setTitle("注册");
		dialog.add(jp1,BorderLayout.NORTH);
		dialog.add(jp,BorderLayout.CENTER);
		dialog.add(jp2,BorderLayout.SOUTH);
		
		
		registBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				textField1.setText("");
				textField2.setText("");
				textField3.setText("");
				textField4.setText("");
				dialog.setVisible(true);
				}
			});
		
		
		

		// 自动设定组件、组之间的间隙
		//layout.setAutoCreateGaps(true);
		//layout.setAutoCreateContainerGaps(true);

		// LEADING -- 左对齐 BASELINE -- 底部对齐 CENTER -- 中心对齐
		//ParallelGroup hpg = layout.createParallelGroup(GroupLayout.Alignment.LEADING);
		//hpg.addComponent(userNaemLabel);
		//hpg.addComponent(userNameText);
		//hpg.addGroup(layout.createSequentialGroup().addGap(50,50,50).addComponent(userNaemLabel).addGap(50,50,50).addComponent(passwordLabel).addGap(50,50,50).addComponent(loginBtn));
		//hpg.addGroup(layout.createSequentialGroup().addGap(50,50,50).addComponent(userNameText).addGap(50,50,50).addComponent(passwordText).addGap(50,50,50).addComponent(registBtn));

		//ParallelGroup vpg = layout.createParallelGroup(GroupLayout.Alignment.BASELINE);
		//vpg.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(userNaemLabel).addGap(100).addComponent(userNameText));
		//vpg.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(passwordLabel).addGap(100).addComponent(passwordText));
		//vpg.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(loginBtn).addGap(100).addComponent(registBtn));
		//vpg.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING).addComponent(userNaemLabel).addComponent(userNameText));
		//vpg.addGap(100).addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER).addComponent(passwordLabel).addComponent(passwordText));
		//vpg.addGap(100).addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(loginBtn).addComponent(registBtn));

		// 水平
		//layout.setHorizontalGroup(hpg);

		// 设定两个Button在水平方向一样宽
		//layout.linkSize(SwingConstants.HORIZONTAL, new Component[] {loginBtn, registBtn });

		// 垂直
		//layout.setVerticalGroup(vpg);
		//setBounds(300, 300, 500, 450);
		setLocation(300,300);
		pack();
		setVisible(true);
	}
	private void refreshRegcode(){
		registrandom  = client.genRegistRandom();
		String[] rs = registrandom.split("-");
		textField1.setText(rs[0]);
		textField2.setText(rs[1]);
		textField3.setText(rs[2]);
		textField4.setText(rs[3]);
	}
	public static void main(String[] args) {
		new ClientMain();
	}
}
